package sampleuiplugin;

import gamefw.framework.Coordinate;
import gamefw.uiplugin.IVisualResource;
import gamefw.uiplugin.IOutputManager;

public class SampleOutputManager implements IOutputManager {
	
	private char[][] currentBoard;

	@Override
	public void beginDraw() {
		this.currentBoard = null;
	}

	@Override
	public void endDraw() {
		if (this.currentBoard != null) {
			for (char[] line : this.currentBoard) {
				System.out.println(new String(line));
			}
		}
	}

	@Override
	public void draw(Coordinate offset, IVisualResource visualResource) {
		int x = offset.getX();
		int y = offset.getY();
		
		if (!(visualResource instanceof SampleVisualResource)) {
			return;
		}

		SampleVisualResource resource = (SampleVisualResource)visualResource;
		char[][] clonedResource = resource.getBoardData().clone();
		
		if (this.currentBoard == null) {
			this.currentBoard = new char[clonedResource.length + y][clonedResource[0].length + x];
			
			for (int row = 0; row < this.currentBoard.length; ++row) {
				if (row < y) {
					for (int col = 0; col < this.currentBoard[row].length; ++col) {
						this.currentBoard[row][col] = ' ';
					}
				} else {
					for (int col = 0; col < x; ++col) {
						this.currentBoard[row][col] = ' ';
					}
					for (int col = 0; col < clonedResource[row - y].length; ++col) {
						this.currentBoard[row][col + x] = clonedResource[row - y][col];
					}
				}
			}
		} else {
			for (int row = 0; row < offset.getY() + this.currentBoard.length && row < clonedResource.length; ++row) {
				for (int col = 0; col < offset.getX() + this.currentBoard[0].length && col < clonedResource[row].length; ++col) {
					this.currentBoard[row + offset.getY()][col + offset.getX()] = clonedResource[row][col];
				}
			}
		}
	}

	@Override
	public void printString(String message) {
		System.out.println();
		System.out.println(message);
		System.out.println();
	}

}
